﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Accounts_VoucherDisplay_Purchase_View : System.Web.UI.Page
{
    Datautility du = new Datautility();
    double grandtotal_dr = 0;
    double grandtotal_cr = 0;
    string strsql="";
    DataTable dtmain = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //**************Set Minimum and maximum date calender Control********
            AccountSettings.setMinMaxDate(form1);
            //********************End **********
            GetFinanceYear();
        }
    }
    protected void GetFinanceYear()
    {
        string Fromdate = "01/04/" + Session["FINYEAR"].ToString();
        string ToDate = "31/03/" + (Convert.ToDouble(Session["FINYEAR"]) + 1).ToString();
        txtfrom.Text = Fromdate.ToString();
        txtto.Text = ToDate.ToString();
        BindDataList(Fromdate, ToDate);
    }
    protected void BindDataList(string fromdate, string todate)
    {
        if (dtmain.Rows.Count > 0)
        {
            foreach (DataRow dr in dtmain.Rows)
            {
                dr.Delete();
            }
        }
        string[] str = fromdate.ToString().Split('/');
        string FromDateMain = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
        string[] str1 = todate.ToString().Split('/');
        string ToDateMain = str1[1].ToString() + "/" + str1[0].ToString() + "/" + str1[2].ToString();
        SqlParameter[] parm = {
                               new SqlParameter("@TransactionTypeID","7"), // 7 for purchase 
                               new SqlParameter("@FromDate",FromDateMain),
                               new SqlParameter("@todate",ToDateMain)
                              };
        dtmain = du.getdatausingprocedure("USP_ACC_GetVoucherViewDetails", parm);
        if (dtmain.Rows.Count > 0)
        {
            DtlPurchase.Visible = true;
            divMessage.Visible = false;
            DataTable DT = new DataTable();
            string[] columns = { "TransactionId", "TransactionDate", "TransactionNumber", "ChequeNumber", "Narration" };
            DT = dtmain.DefaultView.ToTable(true, columns);
            DtlPurchase.DataSource = DT;
            DtlPurchase.DataBind();
        }
        else
        {
            DtlPurchase.Visible = false;
            divMessage.Visible = true;
        }
    }
    protected void btnSearch_period_Click(object sender, EventArgs e)
    {
        BindDataList(txtfrom.Text.ToString(), txtto.Text.ToString());
    }
 
    protected void DtlPurchase_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        grandtotal_cr = 0;
        grandtotal_dr = 0;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal ltrnarration = (Literal)e.Item.FindControl("ltrnarration");
            Literal ltrnarration_name = (Literal)e.Item.FindControl("ltrnarration_name");
            HiddenField htrans_id = (HiddenField)e.Item.FindControl("htrans_id");
            GridView GrdPurchase = (GridView)e.Item.FindControl("GrdPurchase");
            DataTable dt = (DataTable)dtmain;
            SqlParameter[] parm = 
                                    {  
                                        new SqlParameter("@tranid", htrans_id.Value.ToString()) 
                                    };
            DataTable dtTransactionTypeID = du.getdatausingprocedure("usp_get_transactiontype", parm);
            if (dtTransactionTypeID.Rows.Count > 0)
            {
                if (dtTransactionTypeID.Rows[0]["typeid"].ToString() != string.Empty)
                {
                    string from = "";
                    string to = "";
                    string[] d1 = txtfrom.Text.ToString().Split('/');
                    from = d1[1].ToString() + "/" + d1[0].ToString() + "/" + d1[2].ToString();
                    string[] d2 = txtto.Text.ToString().Split('/');
                    to = d2[1].ToString() + "/" + d2[0].ToString() + "/" + d2[2].ToString();


                    HyperLink hyp_print = (HyperLink)e.Item.FindControl("hyp_print");
                    hyp_print.Attributes.Add("onclick", "go('" + htrans_id.Value.ToString() + "','" + from + "','" + to + "','" + dtTransactionTypeID.Rows[0]["typeid"].ToString() + "');");
                }
            }
            if (dt.Rows.Count > 0)
            {
                DataView DView = dt.DefaultView;
                DView.RowFilter = "TransactionID='" + htrans_id.Value + "'";
                DataTable DT1 = DView.ToTable();
                GrdPurchase.DataSource = DT1;
                GrdPurchase.DataBind();
            }          
        }
    }


    protected void GrdPurchase_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdbind = (GridView)sender;
        DataListItem dtlist = (DataListItem)grdbind.Parent;    
         if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal debit = (Literal)e.Row.Cells[1].FindControl("ltrdebit");
            Literal credit = (Literal)e.Row.Cells[1].FindControl("ltrcredit");

            
            string dk = grdbind.DataKeys[e.Row.RowIndex].Value.ToString();
            strsql = "select amount from acc_voudetails where vouserial='" + dk.ToString() + "'";
            DataTable DT = du.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (DT.Rows[0]["amount"].ToString() != string.Empty)
                {
                    Literal dr = (Literal)e.Row.Cells[1].FindControl("ltrdr_cr");
                    if (Convert.ToDouble(DT.Rows[0]["amount"].ToString()) > 0)
                    {
                        dr.Text = "Cr";
                    }
                    else
                    {
                        dr.Text = "Dr";
                    }
                }
            }
            if (Convert.ToDouble(debit.Text) == 0)
            {
                debit.Text = "";
            }
            else
            {
                debit.Text = Math.Abs(Convert.ToDouble(debit.Text)).ToString("N2");
                grandtotal_dr += Convert.ToDouble(debit.Text);
            }
            if (Convert.ToDouble(credit.Text) == 0)
            {
                credit.Text = "";
            }
            else
            {
               credit.Text= Math.Abs(Convert.ToDouble(credit.Text)).ToString("N2");
               grandtotal_cr += Convert.ToDouble(credit.Text);
            }
            
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            ((Literal)e.Row.FindControl("ltrtotdr")).Text = grandtotal_dr.ToString("N2");
            ((Literal)e.Row.FindControl("ltrtotcr")).Text = grandtotal_cr.ToString("N2");
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
        }
    
    }
}
